<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Model;
use think\Db;
use think\Config;
use think\Controller;
use think\Session;
use think\Cookie;
use Godok\Org\Auth;
class Faq extends Common
{
	public function index(){
        $b = Config::get('auth.table_user');
        $where = 'a.is_delete = 1';
        if(input('keywords')!=''){
        	$keywords = input('keywords');
        	$where .= ' AND a.question LIKE '."'%".$keywords."%' ".' OR a.answer LIKE'."'%".$keywords."%'";
        }
       $faq=Db::name('faq')
		->alias('a')
		->join("$b b",'a.create_user = b.id')
		->field('a.id,a.question,a.answer,a.create_time,a.is_delete,b.username')
		->where($where)
		->select();
       	$this->assign('list',$faq);
       	
		return $this->fetch('../application/publicview/faq/index.html');
	}

	public function add(){

		if ( Request::instance()->isPost()) {
			$data['oid'] = 1;
			$data['question'] = input('question');
			$data['answer'] = input('answer');
			$data['create_user'] = 1;
			$data['create_time'] = time();
			$data['is_delete'] = 1;
			$where['question'] = $data['question'];
			$check = Db::name('faq')->where($where)->find();
			if(!$check){
				if(Db::name( 'faq')->insert($data)){
					$oid = 1;$m = 'ausadmin'; $c = 'Faq'; $action="add"; $uid=session('adminid'); $querystring = '添加帮助中心 '.input('question').' 内容';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
	    			$this->redirect('index');
	    		}else{
	    			$this->error(lang('添加的问题已存在'));
	    		}
	    		return;
			}else{
				$this->error(lang('添加失败！'));
			}

		}

		return $this->fetch('../application/publicview/faq/add.html');
	}

	public function edit($id='', $data= ''){
		 $id = Request::instance()->get('id','','number_int');	
		 $where['id'] = $id;
		  $faq = Db::name('faq')->where($where)->find();
		  $this->assign('faq',$faq);	
         //echo $id; die;
		if ( Request::instance()->isPost() ) {
			$data = input('post.');
			if(Db::name( 'faq')->update($data)){
				$oid = 1;$m = 'ausadmin'; $c = 'Faq'; $action="edit"; $uid=session('adminid'); $querystring = '修改帮助中心 '.$id.' 内容';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			$this->redirect('index');
    			//return ['code'=>0, 'msg'=>'修改成功！',''];
    		}else{
    			$this->error(lang('问题重复'));
    		}
    		return;	
		}
		return $this->fetch('../application/publicview/faq/edit.html');
	}

	public function delete($id='', $data= ''){

		$id =  Request::instance()->get('id','','number_int');
		//echo $id;die;
		if($id){
			$data['id'] = $id;
			$data['is_delete'] =0;
			if(Db::name( 'faq')->update($data)){
    			//$this->redirect('index');
    			$oid = 1;$m = 'ausadmin'; $c = 'Faq'; $action="delete"; $uid=session('adminid'); $querystring = '删除帮助中心 '.$id.' 的内容';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>lang('删除成功！')];
    		}else{
    			return ['code'=>-1050, 'msg'=>lang('删除失败！')];
    		}

		}

	}


}	